<template>
  <div class="payment">
    <!-- 支付头部 -->
    <van-nav-bar
      title="订单支付"
      left-arrow
      @click-left="goBack"
    />

    <!-- 支付信息 -->
    <div class="payment-info">
      <div class="order-summary">
        <div class="store-info">
          <img :src="orderInfo.store_logo" :alt="orderInfo.store_name" />
          <span class="store-name">{{ orderInfo.store_name }}</span>
        </div>
        <div class="product-info">
          <img :src="orderInfo.product_image" :alt="orderInfo.product_name" />
          <div class="product-details">
            <h4>{{ orderInfo.product_name }}</h4>
            <p class="shares">购买{{ orderInfo.shares }}份</p>
          </div>
        </div>
      </div>

      <!-- 支付金额 -->
      <div class="payment-amount">
        <div class="amount-label">支付金额</div>
        <div class="amount-value">¥{{ orderInfo.total_amount }}</div>
      </div>

      <!-- 支付方式 -->
      <div class="payment-methods">
        <div class="section-title">选择支付方式</div>
        <van-radio-group v-model="selectedPaymentMethod">
          <van-cell-group>
            <van-cell>
              <template #title>
                <div class="payment-method">
                  <div class="payment-icon wechat-icon">微</div>
                  <span>微信支付</span>
                </div>
              </template>
              <template #right-icon>
                <van-radio name="wechat" />
              </template>
            </van-cell>
            <van-cell>
              <template #title>
                <div class="payment-method">
                  <div class="payment-icon alipay-icon">支</div>
                  <span>支付宝</span>
                </div>
              </template>
              <template #right-icon>
                <van-radio name="alipay" />
              </template>
            </van-cell>
          </van-cell-group>
        </van-radio-group>
      </div>
    </div>

    <!-- 支付按钮 -->
    <div class="payment-actions">
      <van-button 
        type="primary" 
        size="large" 
        block
        :loading="paying"
        @click="handlePayment"
      >
        {{ paying ? '支付中...' : `立即支付 ¥${orderInfo.total_amount}` }}
      </van-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { showToast, showSuccessToast } from 'vant'

const route = useRoute()
const router = useRouter()

// 支付状态
const paying = ref(false)
const selectedPaymentMethod = ref('wechat')

// 订单信息
const orderInfo = ref({
  id: 0,
  store_name: '',
  store_logo: '',
  product_name: '',
  product_image: '',
  shares: 0,
  total_amount: 0
})

// 获取订单信息
const getOrderInfo = () => {
  const orderId = route.params.orderId
  if (!orderId) {
    showToast('订单信息不存在')
    goBack()
    return
  }

  // 这里应该从API获取订单详情，暂时使用路由参数
  const orderData = route.query
  orderInfo.value = {
    id: Number(orderId),
    store_name: String(orderData.store_name || ''),
    store_logo: String(orderData.store_logo || ''),
    product_name: String(orderData.product_name || ''),
    product_image: String(orderData.product_image || ''),
    shares: Number(orderData.shares || 0),
    total_amount: Number(orderData.total_amount || 0)
  }
}

// 处理支付
const handlePayment = async () => {
  if (!selectedPaymentMethod.value) {
    showToast('请选择支付方式')
    return
  }

  paying.value = true

  try {
    // 模拟支付过程
    await new Promise(resolve => setTimeout(resolve, 2000))
    
    // 调用后端支付API
    const response = await fetch(`/api/orders/${orderInfo.value.id}/pay`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        payment_method: selectedPaymentMethod.value
      })
    })

    if (response.ok) {
      showSuccessToast('支付成功！')
      // 跳转到订单页面
      setTimeout(() => {
        router.push('/orders')
      }, 1500)
    } else {
      throw new Error('支付失败')
    }
  } catch (error) {
    console.error('支付错误:', error)
    showToast('支付失败，请重试')
  } finally {
    paying.value = false
  }
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 页面加载时获取订单信息
onMounted(() => {
  getOrderInfo()
})
</script>

<style scoped>
.payment {
  background: #f5f5f5;
  min-height: 100vh;
}

.payment-info {
  padding: 15px;
}

.order-summary {
  background: white;
  border-radius: 12px;
  padding: 15px;
  margin-bottom: 15px;
}

.store-info {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.store-info img {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  object-fit: cover;
  border: 1px solid #eee;
}

.store-name {
  font-size: 14px;
  font-weight: bold;
}

.product-info {
  display: flex;
  gap: 12px;
}

.product-info img {
  width: 70px;
  height: 70px;
  border-radius: 8px;
  object-fit: cover;
  border: 1px solid #eee;
}

.product-details h4 {
  margin: 0 0 8px 0;
  font-size: 14px;
  line-height: 1.4;
}

.product-details .shares {
  margin: 0;
  font-size: 12px;
  color: #666;
}

.payment-amount {
  background: white;
  border-radius: 12px;
  padding: 20px 15px;
  margin-bottom: 15px;
  text-align: center;
}

.amount-label {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.amount-value {
  font-size: 32px;
  font-weight: bold;
  color: #ff6b6b;
}

.payment-methods {
  background: white;
  border-radius: 12px;
  padding: 15px;
  margin-bottom: 15px;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 15px;
}

.payment-method {
  display: flex;
  align-items: center;
  gap: 12px;
}

.payment-icon {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
  color: white;
}

.wechat-icon {
  background: #07c160;
}

.alipay-icon {
  background: #1677ff;
}

.payment-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 15px;
  background: white;
  border-top: 1px solid #eee;
}
</style> 